const app = getApp()
import { getCurrent ,update } from "../../api/company"
const util = require('../../utils/util.js')

Page({
    data: {
        company: {
            linkman: '立即登录',
            phone: '',
            logo: ''
        },
        token:null
    },
    onShow() {
        app.globalData.token =  wx.getStorageSync('token')
        this.setData({
            token:app.globalData.token
        })
        this.getCurrent()
    },
    onChooseAvatar(e){
        this.data.company.logo = e.detail.images[0]
        this.setData({
            company:this.data.company
        })
        let params = {
            "logo": this.data.company.logo,
            "id": this.data.company.id,
        }
        update(params).then(res =>{
            wx.navigateBack({
                delta: 1
            })
        })
        
 
    },
    getCurrent(){
        getCurrent().then(res =>{
            if(res.type == 'logout'){
                this.data.company = {
                    linkman: '立即登录',
                    phone: '',
                    logo: ''
                }
                this.setData({
                    company: this.data.company
                })
                return false
            }
            if(res.data.company.linkman == null){
                res.data.company.linkman = ''
            }
            if(res.data.company.phone == null){
                res.data.company.phone = ''
            }
            res.data.company.logo = util.thumbnail(res.data.company.logo,340,340)
            this.data.company = res.data.company
            this.setData({
                company: this.data.company
            })
        })
    },
    
    avarorClick(){
        if(app.globalData.token == null || app.globalData.token == ""){
            wx.navigateTo({
                url: '/pages/safety/login',
            })
        }else {
            wx.navigateTo({
                url: '/pages/pagesUser/companyInfo/companyInfo',
            })
        }
    },
    tologin(){
        if(app.globalData.token == null){
            wx.navigateTo({
              url: '/pages/safety/login',
            })
        }
    },
    toMyGood(){
        if(app.globalData.token == null){
            wx.navigateTo({
              url: '/pages/safety/login',
            })
           }else if(app.globalData.token != null){
            wx.navigateTo({
                url: '/pages/product/product',
            })
        }
    },
    goToOilStore(){
        if(app.globalData.token == null){
            wx.navigateTo({
              url: '/pages/safety/login',
            })
           }else if(app.globalData.token != null){
            wx.navigateTo({
                url: '/pages/oilStore/oilStore',
              })
        }
    },
    toOrder(){
        if(app.globalData.token == null){
            wx.navigateTo({
              url: '/pages/safety/login',
            })
           }else if(app.globalData.token != null){
            wx.switchTab({
                url: '/pages/order/order',
            })
        }
    },
    goToCustomer(){
        if(app.globalData.token == null){
            wx.navigateTo({
              url: '/pages/safety/login',
            })
           }else if(app.globalData.token != null){
            wx.navigateTo({
                url: '/pages/customer/customer',
              })
        }
    },
    toCompany(){
        if(app.globalData.token == null){
            wx.navigateTo({
                url: '/pages/safety/login',
            })
        }else if(app.globalData.token != null){
            wx.navigateTo({
                url: '/pages/pagesUser/companyInfo/companyInfo',
            })
        }
    },
    goToCustomerManager(){
        if(app.globalData.token == null){
            wx.navigateTo({
                url: '/pages/safety/login',
            })
        }else if(app.globalData.token != null){
            wx.navigateTo({
                url: '/pages/customer/customerManager',
            })
        }
    }
})
